<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>双向链表</title>
</head>
<body>
<script>
    var Node= function (elm) {
        this.elm=elm;
        this.prev=null;
        this.next=null;
    };


    function List(){
        this.head=new Node('head');
        this.pos=this.head;
    }
    List.prototype={
        constructor:List,
        each: function (cb) {
            var curr=this.head;
            do{
                cb(curr);
            }while(curr=curr.next);
        },
        find: function (item) {
            var curr=this.head;
            do{
                if(curr.elm==item){
                    this.pos=curr;
                    return curr;
                }
            }while(curr=curr.next);
            return null;
        },
        findLast: function () {
            var curr=this.head;
            while(curr.next!=null){
                this.pos=curr;
                curr=curr.next;
            }
            return curr;
        },
        display: function () {
            console.info('display:');
            return console.log(this);
        },
        insert: function (newElm,target) {
            var curr=this.find(target),newNode;
            if(!curr)return false;
            newNode=new Node(newElm);
            newNode.next=curr.next;
            newNode.prev=curr;
            curr.next=newNode;
            this[newElm]=newNode;
            this.pos=newNode;
            return true;
        },
        remove: function (elm) {
            var del=this.find(elm);
            if(!del)return false;
            del.prev.next=del.next;
            del.next.prev=del.prev;
            this.pos=del.prev;
            delete this[elm];
            return true;
        },
        getPos: function () {
            return this.pos;
        },
        back: function (n) {
            var curr=this.getPos();
            console.info('back:');
            console.log(n,curr);
            if(!curr || n<0)return false;
            while(n--&&curr.next){
                curr=curr.next;
            }
            console.log('back last:',curr);
        },
        advance: function (n) {
            var curr=this.getPos(),origin=curr;
            console.info('advance:');
            console.log(n,curr);
            if(!curr || n<0)return false;
            while(n--){
                if(!curr.prev)return false;
                curr=curr.prev;
            }
            console.log('advance last:',curr,origin);
            if(curr==origin)return true;
            origin.prev.next=origin.next;

            if(origin.next){
                origin.next.prev=origin.prev;
            }

            if(curr.prev){
                curr.prev.next=origin;
                origin.prev=curr.prev;
            }else{
                origin.prev=null;
            }
            origin.next=curr;
            curr.prev=origin;
            console.log('ok');
        }
    };

    var list=new List();
    list.insert('1','head');
    list.insert('2','1');
    list.insert('3','2');
    list.advance(1);

    list.display();
    //list.display();

    //list.back(2);

</script>
</body>
</html>